﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WK.Libraries.BetterFolderBrowserNS.Helpers;

namespace configfile_wpf
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        string lastSelectPath = string.Empty;
        public MainWindow()
        {
            InitializeComponent();
            this.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
            this.btnClose.Click += (se, ee) => {
                this.Close();
            };
            this.btnMax.Checked += (se, ee) => {
                this.WindowState = WindowState.Maximized;
                this.btnMax.Content = "\ue621";
            };
            this.btnMax.Unchecked += (se, ee) => {
                this.WindowState = WindowState.Normal;
                this.btnMax.Content = "\ue690";
            };
            this.btnMin.Click += (se, ee) => {
                this.WindowState = WindowState.Minimized;
            };
            this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
            this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
            this.btnOpen.Click += BtnOpen_Click;
        }

        private void BtnOpen_Click(object sender, RoutedEventArgs e)
        {
            OutputText.Text = "";
            BetterFolderBrowserDialog folderBrowserDialog = new BetterFolderBrowserDialog();
            folderBrowserDialog.Title = "选择文件夹";
            folderBrowserDialog.InitialDirectory = "C:\\";
            folderBrowserDialog.ShowDialog();
            if (folderBrowserDialog.FileName == string.Empty) {
                MessageBox.Show("请选择文件夹");
                return;
            }
            text.Text = lastSelectPath = folderBrowserDialog.FileName;
            listDirectory(lastSelectPath.Trim());
        }
        string folderName = string.Empty;
        /// <summary>
        /// 列出path路径对应的文件夹中的子文件夹和文件
        /// 然后再递归列出子文件夹内的文件和文件夹
        /// </summary>
        /// <param name="path">需要列出内容的文件夹的路径</param>
        /// <param name="leval">当前递归层级，用于控制输出前导空格的数量</param>
        private void listDirectory(string path)
        {
            try {
                DirectoryInfo theFolder = new DirectoryInfo(@path);
                foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) {
                    var folderpath = AppDomain.CurrentDomain.BaseDirectory;
                    CopyFolder2(folderpath + "\\config", NextFolder.FullName);
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 复制文件夹及文件
        /// </summary>
        /// <param name="sourceFolder">原文件路径</param>
        /// <param name="destFolder">目标文件路径</param>
        /// <returns></returns>
        public int CopyFolder2(string sourceFolder, string destFolder)
        {
            try {
                string folderName = System.IO.Path.GetFileName(sourceFolder);
                string destfolderdir = System.IO.Path.Combine(destFolder, folderName);
                string[] filenames = System.IO.Directory.GetFileSystemEntries(sourceFolder);
                foreach (string file in filenames)// 遍历所有的文件和目录
                {
                    if (System.IO.Directory.Exists(file)) {
                        string currentdir = System.IO.Path.Combine(destfolderdir, System.IO.Path.GetFileName(file));
                        if (!System.IO.Directory.Exists(currentdir)) {
                            System.IO.Directory.CreateDirectory(currentdir);
                        }
                        CopyFolder2(file, destfolderdir);
                    } else {
                        string srcfileName = System.IO.Path.Combine(destfolderdir, System.IO.Path.GetFileName(file));
                        if (!System.IO.Directory.Exists(destfolderdir)) {
                            System.IO.Directory.CreateDirectory(destfolderdir);
                        }
                        System.IO.File.Copy(file, srcfileName);
                    }
                }
                OutputText.Text += destFolder + "___success\r\n";

                return 1;
            } catch (Exception e) {

                MessageBox.Show(e.Message);
                return 0;
            }
        }


        private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
